package com.dudu.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.ISmsCodeService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.SmsCodeServiceImpl;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.Encrypt;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.HtmlUtils;
import com.dudu.utils.PhoneUtils;

import core.CoreService;
import core.util.Const;
/**
 * 发验证码,web 通话工具
 * @author gaoxs
 *
 */
public class OnlineConsultController extends CoreService implements IOnlineConsultController {
	//短信
	private ISmsCodeService smsCodeServcie=new SmsCodeServiceImpl();
	//app扩展信息操作
	private IAppExtendService appExtendService = new AppExtendServiceImpl();
	//账号扩展信息操作
	private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
	/**
	 * 获取验证码
	 */
	@Override
	public String getCode() {
		String callback = this.getRequest().getParameter("callback");
		JSONObject object = new JSONObject();
		String appid = this.getRequest().getParameter("appid");
		String phone = this.getRequest().getParameter("phone");
		// 校验手机号码是否非法
		if (PhoneUtils.isMobilePhone(phone) && StringUtils.isNotBlank(appid)) {
			String result = smsCodeServcie.sendCode(phone);
				log.info("UserAction getCode() 发送短信验证码");
				if("0".equals(result)){
					object.put("result", "0");
					object.put("discripe", "验证码已发送，请注意查收");
				}else{
					object.put("result", "-1");
					object.put("discripe", "获取验证码失败，请重新获取");
				}
		} else {
			object.put("result", "-1");
			object.put("discripe", "参数错误");
		}
		log.info("------"+object.toString());
		String data = "";
		if(StringUtils.isNotBlank(callback)){
			data = callback + "(" + object.toString() + ")";
		}else{
			data = object.toString();
		}
		return returnData(data);

	}
	/**
	 * 两方通话js入口
	 * @return
	 */
	public String onlineTwonessCommunicate(){
		String app_key=this.getRequest().getParameter("app_key");
		if (EnumUtility.isBlank(app_key)) {// 未传入应用标识
			return sendResponse(setParaMap("0001","应用标识为空"));
		}
		String cust_account=this.getRequest().getParameter("cust_account");
		if (EnumUtility.isBlank(cust_account)) {// 未传入账号标识
			return sendResponse(setParaMap("0018","账号标识不存在"));
		}
		//查询应用信息
		AppExtendBean appExtendBean=appExtendService.queryAppExtendByIdentify(app_key);
		if(EnumUtility.isBlank(appExtendBean)){
			return sendResponse(setParaMap("0009","应用标识不存在"));
		}
		//根据账号cust_account查询账号信息
		AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		if(EnumUtility.isBlank(accountBean)){
			return sendResponse(setParaMap("0010","账号标识不存在"));
		}
		String show=this.getRequest().getParameter("show");
		/*
		 * 如果没有传递参数show，默认为off，也就是需要点击按钮后才展示在线咨询页面
		 */
		if(StringUtils.isEmpty(show)){
			show="off";
		}
		String basepath=DictionaryUtils.getValueByTypeAndKey("common", "domainUrl");
		String phones = accountBean.getLinker_phone();
		Map<String, String> variables = new HashMap<String, String>();
		String requestTimeStamp = String.valueOf(new Date().getTime());
		String sign = Encrypt.sha1Encrypt(cust_account + accountBean.getKey_private() + requestTimeStamp);
		variables.put("app_key", app_key);
		variables.put("cust_account", cust_account);
		variables.put("timestamp", requestTimeStamp);
		variables.put("sign", sign);
		variables.put("phones", phones);
		variables.put("show", show);
		variables.put("basepath", basepath);
		return sendResponse(variables);
	}
	/**
	 * 设置参数
	 * @param code
	 * @param desc
	 * @return
	 */
	private Map<String, String> setParaMap(String code,String desc){
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", code);
		result.put("describe", desc);
		return result;
	}
	/**
	 * 返回响应结果，格式为js语句
	 * @param variables 需要直接赋值的变量集合
	 * @return 
	 */
	private String sendResponse(Map<String, String> variables) {
		String script_url = DictionaryUtils.getValueByTypeAndKey("h5", "otcScriptUrl");
		return returnData(
				HtmlUtils.createAssignmentScript(variables) + HtmlUtils.importScript(new String[] { script_url }));
	}
	/**
	 * 两方通话接口封装
	 * @return
	 */
	public String createTwonessCommunicate(){
		HttpServletRequest request = this.getRequest();
		String callback = request.getParameter("callback");
		String code = request.getParameter("randCode");
		HttpSession session = request.getSession();
		String sessioncode=(String)session.getAttribute(Const.CODE_IMG);
		JSONObject result = new JSONObject();
		if(!code.equals(sessioncode)){
			result.put("code", "0023");
			result.put("describe", "验证码输入错误");
		}else{
			result.put("code", "0");
			result.put("describe", "成功");
		}
		if (StringUtils.isEmpty(callback))
			return returnData(result.toString());
		else
			return returnData(callback + "(" + result.toString() + ")");
		//return toForword(DemoController.class, "dail" );
		//return this.toRedirect(DemoController.class, "dail2");
	}
}
